package com.meibao.sms.mq;

import com.meibao.common.utils.JsonUtils;
import com.meibao.sms.config.SmsProperties;
import com.meibao.sms.utils.SmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.util.CollectionUtils;

import java.util.Map;
@Slf4j
//@Component
@EnableConfigurationProperties(SmsProperties.class)
public class SmsListener {

    @Autowired
    private SmsProperties prop;
    @Autowired
    private SmsUtils smsUtils;

    /**
     * 发送短信验证码类型
     * @param
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "sms.verify.code.queue", durable = "true"),
            exchange = @Exchange(name = "mb.sms.exchange", type = ExchangeTypes.TOPIC),
            key = "sms.verify.code"
    ))
    public void listenInsertOrUpdate(Map<String, String> msg){
        if(CollectionUtils.isEmpty(msg)){
            return ;
        }
        String phone = msg.remove("phone");
        if(StringUtils.isNotBlank(phone)){
            return;
        }
        //处理消息
        smsUtils.sendSms(phone, prop.getSignName(), prop.getVerifyCodeTemplate(), JsonUtils.serialize(msg));

        //日志记录
        log.info("[短信服务], 发送短信验证码，手机号：{}", phone);
    }

}
